<?php
/* SVN FILE: $Id$ */
/**
 * Travian Alliance Coordinator
 * Copyright (c) 2009, Travian Alliance Coordinator Team.
 *
 * This file is part of Travian Alliance Coordinator.
 *
 * Travian Alliance Coordinator is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Travian Alliance Coordinator is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Travian Alliance Coordinator.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author $Author$
 * @copyright Copyright (c) 2009, Travian Alliance Coordinator Team.
 * @version $Revision$
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3
 */

class RememberMeComponent extends Object {
    var $components = array('Auth', 'Cookie');
    var $controller = null;

    /**
     * Cookie retention period.
     *
     * @var string
     */
    var $period = '+2 weeks';
    var $cookieName = 'User';

    function startup(&$controller) {
        $this->controller =& $controller;
    }

    function remember($username, $password) {
        $cookie = array();
        $cookie[$this->Auth->fields['username']] = $username;
        $cookie[$this->Auth->fields['password']] = $password;

        $this->Cookie->write(
            $this->cookieName,
            $cookie,
            true,
            $this->period
        );
    }

    function check() {
        $cookie = $this->Cookie->read($this->cookieName);

        if (!is_array($cookie) || $this->Auth->user())
        return;

        if ($this->Auth->login($cookie)) {
            $this->Cookie->write(
                $this->cookieName,
                $cookie,
                true,
                $this->period
            );
        }
        else
        {
            $this->delete();
        }
    }

    function delete() {
        $this->Cookie->del($this->cookieName);
    }
}
?>